package com.ruoyi.common.utils.sms;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.util.Random;

/**
 * @Author: MMDZ
 * @Desc:
 */
@Component
public class SendSmsUtil {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${sms.url}")
    private String sms_url;
    @Value("${sms.uid}")
    private String sms_uid;
    @Value("${sms.key}")
    private String sms_key;

    /**
     * @param sendPhoneNum 短信发送目标号码
     * @param content      短信内容
     * @return Integer(1 ： 成功码 ， 其他失败 ， 具体参见注释)
     */
    public String sendSms(String sendPhoneNum, String content) {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(sms_url);
        logger.info("sms_url:" + sms_url);
        // 在头文件中设置转码
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");

        //设置参数
        NameValuePair[] data = {
                new NameValuePair("Uid", sms_uid),//本站用户名
                new NameValuePair("Key", sms_key),//接口安全秘钥
                new NameValuePair("smsMob", sendPhoneNum),//手机号码
                new NameValuePair("smsText", content)//发送内容
        };

        post.setRequestBody(data);

        try {
            client.executeMethod(post);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Header[] headers = post.getResponseHeaders();
        for (Header h : headers) {
            logger.info(h.toString());
        }

        String result = "";
        try {
            result = new String(post.getResponseBodyAsString().getBytes("gbk"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        post.releaseConnection();
        return getMessage(Integer.parseInt(result));
    }

    /**
     * -1  没有该用户账户
     * -2  接口密钥不正确 [查看密钥]不是账户登陆密码
     * -21 MD5接口密钥加密不正确
     * -3  短信数量不足
     * -11 该用户被禁用
     * -14 短信内容出现非法字符
     * -4  手机号格式不正确
     * -41 手机号码为空
     * -42 短信内容为空
     * -51 短信签名格式不正确接口签名格式为：【签名内容】
     * -6  IP限制
     * >0  短信发送数量
     * 以上作为补充
     */
    private String getMessage(Integer code) {
        String message;
        if (code > 0) {
            message = "SMS-f发送成功！短信量还有" + code + "条";
        } else if (code == -1) {
            message = "SMS-没有该用户账户";
        } else if (code == -2) {
            message = "SMS-接口密钥不正确";
        } else if (code == -21) {
            message = "SMS-MD5接口密钥加密不正确";
        } else if (code == -3) {
            message = "SMS-短信数量不足";
        } else if (code == -11) {
            message = "SMS-该用户被禁用";
        } else if (code == -14) {
            message = "SMS-短信内容出现非法字符";
        } else if (code == -4) {
            message = "SMS-手机号格式不正确";
        } else if (code == -41) {
            message = "SMS-手机号码为空";
        } else if (code == -42) {
            message = "SMS-短信内容为空";
        } else if (code == -51) {
            message = "SMS-短信签名格式不正确接口签名格式为：【签名内容】";
        } else if (code == -6) {
            message = "SMS-IP限制";
        } else {
            message = "其他错误";
        }
        return message;
    }

    /**
     * 随机生成6位验证码
     *
     * @return
     */
    public static String getRandomCode(Integer code) {
        Random random = new Random();
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < code; i++) {
            result.append(random.nextInt(10));
        }
        return result.toString();
    }
}
